package com.lx.core.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by lanyxp on 2021/2/23 10:40
 * <br> 项目异步线程池配置 配置参数可以根据实际项目做动态调整</br>
 */
@Configuration
@EnableAsync
public class ExecutorPoolConfig {
    // 初始化核心线程池数量
    private int corePoolSize = 10;
    //最大线程数量
    private int maxPoolSize = 400;
    //缓存队列用来执行缓冲执行的队列
    private int queueCapacitySize = 0;
    //等待时间
    private int awaitTerminationSeconds = 60;

    @Bean
    public ExecutorService AsyncExecutorService() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacitySize);
        executor.setThreadNamePrefix("lx-boot-web-asyncExecutor");
        executor.setAwaitTerminationSeconds(awaitTerminationSeconds);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor.getThreadPoolExecutor();
    }
}
